package com.edou.member.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edou.base.page.PageResult;
import com.edou.base.util.EncryptUtil;
import com.edou.member.dao.IMemberCompanyDao;
import com.edou.member.dao.IMemberUserCompanyDao;
import com.edou.member.dao.IMemberUserDao;
import com.edou.member.dao.IMemberUserRoleDao;
import com.edou.member.dto.MemberUserCondition;
import com.edou.member.entity.MemberCompany;
import com.edou.member.entity.MemberUser;
import com.edou.member.entity.MemberUserCompany;
import com.edou.member.entity.MemberUserRole;
import com.edou.member.service.IMemberUserService;

/**
 * 会员基本信息
 * @creator     
 * @create-time 2016/3/12 16:10:22
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Service("memberUserServiceImpl")
@Transactional(value = "transactionManager")
public class MemberUserServiceImpl implements IMemberUserService{

	@Resource(name = "memberUserDaoImpl")
	private IMemberUserDao memberUserDao;
	
	@Resource(name = "memberCompanyDaoImpl")
	private IMemberCompanyDao memberCompanyDao;
	
	@Resource(name = "memberUserCompanyDaoImpl")
	private IMemberUserCompanyDao memberUserCompanyDao;
	
	@Resource(name = "memberUserRoleDaoImpl")
	private IMemberUserRoleDao memberUserRoleDao;
	
	/**
	 * 查询所有会员基本信息
	 */
	public List<MemberUser> listMemberUserAll(){
		return memberUserDao.listMemberUserAll();
	}

	/**
	 * 分页查询
	 * @param pageResult 分页对象
	 * @param condition 查询条件类
	 */
	public void listMemberUserByPage(PageResult<MemberUser> pageResult,MemberUserCondition condition){
		int rows = memberUserDao.listMemberUserByPageCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<MemberUser> list = memberUserDao.listMemberUserByPage(rowBounds,condition);
		pageResult.setResult(list);
	}

	/**
	 * 根据ID查询
	 * @param id 主键
	 * @return 会员基本信息
	 */
	public MemberUser getMemberUserById(String id){
		return memberUserDao.getMemberUserById(id);
	}
	
	/**
	 * 根据帐号与密码查询会员
	 * 帐号是指：帐号、手机号、邮箱；
	 * 密码是指：加密后的密码
	 * @param account	帐号、手机号、邮箱
	 * @param password	加密后的密码
	 * @return
	 */
	public MemberUser getMemberUserByAccountAndPassword(String account, String password){
		return memberUserDao.getMemberUserByAccountAndPassword(account, password);
	}
	
	/**
	 * 根据会员帐号获得会员信息
	 * @param account	会员帐号
	 * @return
	 */
	public MemberUser getMemberUserByAccount(String account){
		return memberUserDao.getMemberUserByAccount(account);
	}
	
	/**
	 * 根据邮箱查询会员
	 * @param email		邮箱
	 * @return
	 */
	public MemberUser getMemberUserByEmail(String email){
		return memberUserDao.getMemberUserByEmail(email);
	}
	
	/**
	 * 根据手机号码查询会员
	 * @param mobile	手机号码
	 * @return
	 */
	public MemberUser getMemberUserByMobile(String mobile){
		return memberUserDao.getMemberUserByMobile(mobile);
	}

	/**
	 * 新增
	 * @param item 会员基本信息
	 */
	public void add(MemberUser item){
		
		if(StringUtils.isNotBlank(item.getPassword())){
			String pwd = EncryptUtil.encryptionPw(item.getPassword());
			item.setPassword(pwd);
		}
		memberUserDao.add(item);
		
		// 保存会员的公司信息与关联表信息
		MemberCompany company = item.getCompany();
		if(null != company){
			memberCompanyDao.add(company);
			
			// 保存会员与公司信息之间的关联表
			String companyId = company.getId();
			MemberUserCompany memberUserCompany = item.getMemberUserCompany();
			if(null == memberUserCompany){
				memberUserCompany = new MemberUserCompany();
			}
			memberUserCompany.setMemberCompanyId(companyId);
			memberUserCompany.setMemberUserId(item.getId());
			
			memberUserCompanyDao.add(memberUserCompany);
		}
		
		// 保存会员角色
		if(StringUtils.isNotBlank(item.getMemberRoleId())){
			MemberUserRole memberUserRole = new MemberUserRole();
			memberUserRole.setMemberUserId(item.getId());
			memberUserRole.setMemberRoleId(item.getMemberRoleId());
			memberUserRoleDao.add(memberUserRole);
		}
		
		// 保存会员医院
		if(StringUtils.isNotBlank(item.getMemberCompanyHospitalId())){
			MemberUserCompany memberUserCompany = new MemberUserCompany();
			memberUserCompany.setMemberUserId(item.getId());
			memberUserCompany.setMemberCompanyId(item.getMemberCompanyHospitalId());
			memberUserCompanyDao.add(memberUserCompany);
		}
		// 保存会员集团
		if(StringUtils.isNotBlank(item.getMemberCompanyId())){
			MemberUserCompany memberUserCompany = new MemberUserCompany();
			memberUserCompany.setMemberUserId(item.getId());
			memberUserCompany.setMemberCompanyId(item.getMemberCompanyId());
			memberUserCompanyDao.add(memberUserCompany);
		}
		
	}
	
	/**
	 * 开通会员帐号
	 * @param item 会员基本信息
	 */
	public void doOpenMember(MemberUser item, Long clientId){
		try{
			if(StringUtils.isNotBlank(item.getPassword())){
				String pwd = EncryptUtil.encryptionPw(item.getPassword());
				item.setPassword(pwd);
			}
			memberUserDao.add(item);
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

	/**
	 * 修改
	 * @param item 会员基本信息
	 */
	public void update(MemberUser item){
		memberUserDao.update(item);
		MemberCompany company = item.getCompany();
		if(null != company){
			memberCompanyDao.update(company);
			MemberUserCompany memberUserCompany = item.getMemberUserCompany();
			memberUserCompanyDao.update(memberUserCompany);
		}
		// 保存会员角色
		if(StringUtils.isNotBlank(item.getMemberRoleId())){
			MemberUserRole memberUserRole = new MemberUserRole();
			memberUserRole.setMemberUserId(item.getId());
			memberUserRole.setMemberRoleId(item.getMemberRoleId());
			memberUserRoleDao.add(memberUserRole);
		}
	}
	
	/**
	 * 修改会员密码
	 * @param item
	 */
	public void updatePwd(MemberUser item){
		String pwd = EncryptUtil.encryptionPw(item.getPassword());
		item.setPassword(pwd);
		memberUserDao.updatePwd(item);
	}
	
	/**
	 * 修改会员头像
	 * @param item
	 */
	public void updateFacePath(MemberUser item){
		memberUserDao.updateFacePath(item);
	}

	/**
	 * 根据ID集合批量删除
	 * @param ids ID集合
	 */
	public void delByIds(String[] ids){
		memberUserDao.delByIds(ids);
	}

	@Override
	public void updateStates(String[] ids,Integer state) {
		memberUserDao.updateStates(ids,state);
	}

	@Override
	public MemberUser getMemberUserByIdAndMobile(String id, String mobile) {
		return memberUserDao.getMemberUserByIdAndMobile(id, mobile);
	}

	@Override
	public MemberUser getMemberUserByIdAndEmail(String id, String email) {
		return memberUserDao.getMemberUserByIdAndEmail(id, email);
	}

	@Override
	public List<MemberUser> listDayOperateForCreateDate(MemberUserCondition condition) {
		return memberUserDao.listDayOperateForCreateDate(condition);
	}

	@Override
	public List<MemberUser> listWeekOperateForCreateDate(MemberUserCondition condition) {
		return memberUserDao.listWeekOperateForCreateDate(condition);
	}

	@Override
	public List<MemberUser> listMonthOperateForCreateDate(MemberUserCondition condition) {
		return memberUserDao.listMonthOperateForCreateDate(condition);
	}
	
	/**
	 * 上传实名认证所需信息
	 * @param certIdcard
	 * @param certIdcardPath
	 */
	@Override
	public void uploadRealNameCertInfoById(String id,String name,String certIdcard,String certIdcardPath){
		 memberUserDao.uploadRealNameCertInfoById(id,name,certIdcard, certIdcardPath);
	}
	
	/**
	 * 更改改实名认证状态-批量
	 * @param ids
	 * @param certState
	 */
	@Override
	public void modifyCertState(String[] ids,Integer certState){
		 memberUserDao.modifyCertState(ids, certState);
	}

	@Override
	public void updateBalancePoint(MemberUser item) {
		memberUserDao.updateBalancePoint(item);
	}

	/**
	 * 是否禁用账号 0.否 1.是
	 */
	@Override
	public void updateDeleteFlag(MemberUser item) {
		memberUserDao.updateDeleteFlag(item);
	}

}

